<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        li {
            list-style: none;
        }

        .tb-promo {
            position: relative;
            width: 520px;
            height: 280px;
            margin: 50px auto;
            overflow: hidden;
        }

        .tb-promo img {
            width: 520px;
            height: 280px;
        }

        .tb-promo .pics {
            position: absolute;
            width: 600%;
            left: 0px;
        }

        .tb-promo .pics li {
            float: left;
        }

        /* 并集选择器将公共部分放在一起 */
        .prev,
        .next {
            position: absolute;
            top: 50%;
            margin-top: -15px;
            width: 20px;
            height: 30px;
            background-color: rgba(0, 0, 0, 0.2);
            color: #fff;
            text-decoration: none;
            line-height: 30px;
            text-align: center;
        }

        .prev {
            left: 0;
            border-radius: 0% 15px 15px 0;
        }

        .next {
            right: 0;
            border-radius: 15px 0 0 15px;
        }

        .promo-nav {
            display: block;
            position: absolute;
            bottom: 10px;
            left: 50%;
            margin-left: -35px;
            width: auto;
            height: 13px;
            background-color: rgba(255, 255, 255, 0.3);
            border-radius: 6px;
        }

        .promo-nav li {
            float: left;
            margin: 3px;
            width: 8px;
            height: 8px;
            background-color: #fff;
            border-radius: 50%;
        }

        .promo-nav .selected {
            background-color: #ff5000;
        }
    </style>
    <script src="animate.js"></script>
</head>

<body>
    <div class="tb-promo">
        <ul class="pics">
            <li><img src="images/1.jpg" alt=""></li>
            <li><img src="images/3.jpg" alt=""></li>
            <li><img src="images/2.jpg" alt=""></li>
            <li><img src="images/4.jpg" alt=""></li>
            <li><img src="images/4.jpg" alt=""></li>
            <li><img src="images/4.jpg" alt=""></li>
        </ul>

        <!-- 左侧按钮 -->
        <a href="#" class="prev">&lt;</a>
        <!-- 右侧按钮 -->
        <a href="#" class="next">&gt;</a>

        <ul class="promo-nav">
        </ul>
    </div>
</body>
<script>
    var tbpromo = document.querySelector('.tb-promo');
    var btnl = document.querySelector('.prev');
    var btnr = document.querySelector('.next');
    var promonav = document.querySelector('.promo-nav');

    var pics = document.querySelector('.pics');
    var selected = 0;

    var autotimer = setInterval(next, 2000);

    //动态生成按钮li
    for (var i = 0; i < pics.children.length; i++) {
        var li = document.createElement('li');
        li.setAttribute('index', i);
        promonav.appendChild(li);
        li.addEventListener('click', function () {//圆圈按钮点击事件
            //图片变化
            index = this.getAttribute('index');
            animate(pics, -tbpromo.clientWidth * index);
            //按钮颜色变化
            for (var i = 0; i < promonav.children.length; i++) {
                promonav.children[i].className = '';
            }
            this.className = 'selected';
        })
    }
    var btn = promonav.querySelectorAll('li');
    btn[0].className = 'selected';

    //NEXT按钮实现
    function next() {
        if (selected < (btn.length - 1)) {

            animate(pics, -(selected + 1) * tbpromo.clientWidth);
            btn[selected].className = '';
            selected++;
            btn[selected].className = 'selected';
        } else {
            animate(pics, 0);
            btn[selected].className = '';
            selected = 0;
            btn[selected].className = 'selected';

        }
    }
    function prev() {
        if (selected > 0) {
            console.log(selected);
            animate(pics, -(selected - 1) * tbpromo.clientWidth);
            btn[selected].className = '';
            selected--;
            btn[selected].className = 'selected';
        } else {
            animate(pics, -tbpromo.clientWidth * (btn.length - 1));
            btn[selected].className = '';
            selected = (btn.length - 1);
            btn[selected].className = 'selected';
        }

    }

    btnl.addEventListener('click', prev);
    btnr.addEventListener('click', next);

    tbpromo.addEventListener('mouseenter', function () {
        // console.log(1);
        btnl.style.display = 'block';
        btnr.style.display = 'block';
        promonav.style.display = 'block';
        clearInterval(autotimer);
    })
    tbpromo.addEventListener('mouseleave', function () {
        // console.log(1);
        btnl.style.display = 'none';
        btnr.style.display = 'none';
        promonav.style.display = 'none';
        autotimer = setInterval(next, 2000);
    })

</script>

</html>